﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.User.Events
{
    using SharingCenter.Web.Classes;
    public partial class Schedule : EventPageBase
    {
        protected readonly int recentEventDays = 30;
        protected readonly int recentEventMonths = 1;
        protected DateTime displayBeforeDate;
        protected DateTime displayFromDate;
        protected bool displayChanged = false;
        protected List<Sc_Event> eventList;

        protected void Page_Init(object sender, EventArgs e)
        {
            lnkAllEvents.Click += new EventHandler(lnkAllEvents_Click);
            lnkRecentEvents.Click += new EventHandler(lnkRecentEvents_Click);
            //Calendar1.DayRender += new DayRenderEventHandler(Calendar1_DayRender);
        }

        void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            foreach (Sc_Event evt in eventList)
            {
                if (evt.StartDate.Date == e.Day.Date)
                {
                    e.Cell.CssClass = "day highlighted eventday";
                }
                else
                {
                    e.Day.IsSelectable = false;
                    HyperLink tmp = new HyperLink();
                    tmp.Text = e.Day.DayNumberText;
                    tmp.ToolTip = e.Day.Date.ToString("M月d日");
                    e.Cell.Text = "";
                    e.Cell.Controls.Add(tmp);
                    
                }
            }
        }

        void lnkRecentEvents_Click(object sender, EventArgs e)
        {
            displayChanged = false;
            displayFromDate = DateTime.Now;
            displayBeforeDate = DateTime.Today.AddMonths(recentEventMonths);
            BindEventList();
        }

        void lnkAllEvents_Click(object sender, EventArgs e)
        {
            displayChanged = true;
            displayBeforeDate = ((DateTime)ViewState["DISPLAY_BEFORE"]).AddMonths(recentEventMonths);
            displayFromDate = ((DateTime)ViewState["DISPLAY_FROM"]).AddMonths(recentEventMonths);
            BindEventList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                displayFromDate = DateTime.Now;
                displayBeforeDate = DateTime.Today.AddMonths(recentEventMonths);
                BindEventList();
            }
        }

        void BindEventList()
        {
            ViewState["DISPLAY_BEFORE"] = displayBeforeDate;
            ViewState["DISPLAY_FROM"] = displayFromDate;
            ViewState["DISPLAY_CHANGED"] = displayChanged;

            var q = from t in scDataContext.Instance.Sc_Events
                    from o in t.Sc_EventParticipants
                    where displayFromDate < t.StartDate && t.StartDate.Date <= displayBeforeDate && o.UserId == Profile.UserId
                    orderby t.StartDate
                    select t;
            eventList = q.ToList();
            ListView1.DataSource = q;
            
            ListView1.DataBind();
            
        }
    }
}
